package com.xinqi.modules.chat.chat.official.service.impl;

import com.xinqi.common.base.enums.YesNoNumberEnum;
import com.xinqi.common.core.constant.ChatRemindConstants;
import com.xinqi.common.core.enums.AssistantMessageTypeEnum;
import com.xinqi.modules.chat.chat.official.convert.ChatAssistantConfigConvert;
import com.xinqi.modules.chat.chat.official.domain.ChatAssistantConfigEntity;
import com.xinqi.modules.chat.chat.official.dto.req.ChatAssistantConfigUpdateDTO;
import com.xinqi.modules.chat.chat.official.dto.rsp.ChatAssistantConfigResultDTO;
import com.xinqi.modules.chat.chat.official.dto.rsp.ChatAssistantTypeResultDTO;
import com.xinqi.modules.chat.chat.official.manager.ChatAssistantConfigManager;
import com.xinqi.modules.chat.chat.official.service.ChatAssistantConfigService;
import com.xinqi.modules.chat.third.rongyun.service.RongyunService;
import io.rong.util.CodeUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * 新奇助手通知设置表服务层实现
 *
 * @author: yyzhen
 * @date: 2023/05/05
 */
@Service("chatAssistantConfigService")
@RequiredArgsConstructor
public class ChatAssistantConfigServiceImpl implements ChatAssistantConfigService {
    private final ChatAssistantConfigConvert chatAssistantConfigConvert;
    private final ChatAssistantConfigManager chatAssistantConfigManager;

    private final RongyunService rongyunService;


    /**
     * 查询新奇助手通知设置
     *
     * @param userId
     * @return
     */
    @Override
    public ChatAssistantConfigResultDTO infoConfig(Long userId) {
        ChatAssistantConfigEntity configEntity = chatAssistantConfigManager.selectByUserId(userId);
        if (Objects.isNull(configEntity)) {
            //没有则初始化
            chatAssistantConfigManager.initByUserId(userId);
            configEntity = chatAssistantConfigManager.selectByUserId(userId);
        }
        return chatAssistantConfigConvert.convert(configEntity);
    }

    /**
     * 修改新奇助手通知设置
     *
     * @param dto
     * @return
     */
    @Override
    public ChatAssistantConfigResultDTO editConfig(ChatAssistantConfigUpdateDTO dto) {
        ChatAssistantConfigEntity old = chatAssistantConfigManager.selectByUserId(dto.getOperationUserId());
        if (Objects.isNull(old)) {
            //没有则初始化
            chatAssistantConfigManager.initByUserId(dto.getOperationUserId());
            old = chatAssistantConfigManager.selectByUserId(dto.getOperationUserId());
        }
        //免打扰
        if (Objects.nonNull(dto.getIsDisturb())) {
            if (Objects.equals(YesNoNumberEnum.YES.getCode(), dto.getIsDisturb())) {
                boolean muteFlag = rongyunService.conversationDisturb(dto.getOperationUserId().toString(), ChatRemindConstants.ASSISTANT,
                    CodeUtil.ConversationType.PRIVATE, YesNoNumberEnum.YES.getCode());
                if (!muteFlag) {
                    dto.setIsDisturb(YesNoNumberEnum.NO.getCode());
                }
            } else if (Objects.equals(YesNoNumberEnum.NO.getCode(), dto.getIsDisturb())) {
                boolean unMuteFlag = rongyunService.conversationDisturb(dto.getOperationUserId().toString(), ChatRemindConstants.ASSISTANT,
                    CodeUtil.ConversationType.PRIVATE, YesNoNumberEnum.NO.getCode());
                if (!unMuteFlag) {
                    dto.setIsDisturb(YesNoNumberEnum.NO.getCode());
                }
            }
        }
        ChatAssistantConfigEntity configEntity = chatAssistantConfigConvert.update(dto);
        configEntity.setUserId(dto.getOperationUserId());
        configEntity.setId(old.getId());
        chatAssistantConfigManager.updateById(configEntity);
        configEntity = chatAssistantConfigManager.selectByUserId(dto.getOperationUserId());
        return chatAssistantConfigConvert.convert(configEntity);
    }

    /**
     * 获取 消息助手-类型
     *
     * @return
     */
    @Override
    public List<ChatAssistantTypeResultDTO> typeList() {
        List<ChatAssistantTypeResultDTO> list = new ArrayList<>();
        for (AssistantMessageTypeEnum typeEnum : AssistantMessageTypeEnum.values()) {
            if (!Objects.equals(AssistantMessageTypeEnum.ATTENDANCE, typeEnum)
                && !Objects.equals(AssistantMessageTypeEnum.COURSEWARE, typeEnum)) {
                ChatAssistantTypeResultDTO type = new ChatAssistantTypeResultDTO();
                type.setCode(typeEnum.getCode());
                type.setType(typeEnum.getType());
                type.setDescribe(typeEnum.getDescription());
                list.add(type);
            }
        }
        return list;
    }
}
